package com.uking.util;

import android.content.SharedPreferences;

import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
import java.util.TimeZone;

public class TimeUtils {
    public static final String DEFAULT_STR = "yyyy-MM-dd HH:mm:ss";
    public static final SimpleDateFormat DEFAULT_SDF = new SimpleDateFormat(DEFAULT_STR, Locale.getDefault());


    public static void getWebsiteDatetime(final String webUrl, final String cacheDataSp, final SharedPreferences sp) {
        new Thread() {
            public void run() {
                try {
                    if (webUrl != null) {
                        URLConnection uc = new URL(webUrl).openConnection();
                        uc.connect();
                        sp.edit().putString(cacheDataSp, new SimpleDateFormat(TimeUtils.DEFAULT_STR, Locale.CHINA).format(new Date(uc.getDate())));
                        return;
                    }
                    sp.edit().putString(cacheDataSp, null);
                } catch (MalformedURLException e) {
                    e.printStackTrace();
                } catch (IOException e2) {
                    e2.printStackTrace();
                }
            }
        }.start();
    }

    public static String getWebTime(String webUrl) {
        String str = null;
        try {
            URLConnection uc = new URL(webUrl).openConnection();
            uc.connect();
            str = new SimpleDateFormat(DEFAULT_STR, Locale.CHINA).format(new Date(uc.getDate()));
        } catch (MalformedURLException e) {
            Utils.log("\u629b\u5f02\u5e38MalformedURLException");
            e.printStackTrace();
        } catch (IOException e2) {
            Utils.log("\u629b\u5f02\u5e38IOException");
            e2.printStackTrace();
        }
        return str;
    }

    public static void setBJCacheTime(SharedPreferences sharedAAA, String time) {
        sharedAAA.edit().putString(Constants.NEW_REGISTER_CACHE_TIME, time).commit();
    }

    public static String getBeiJingTime(String webUrl) {
        try {
            int timeOut;
            HttpURLConnection connection = (HttpURLConnection) new URL(webUrl).openConnection();
            if (webUrl.equals(Constants.baiduUrl)) {
                timeOut = 10000;
            } else {
                timeOut = 15000;
            }
            connection.setConnectTimeout(timeOut);
            connection.setReadTimeout(timeOut);
            connection.connect();
            long date = connection.getDate();
            SimpleDateFormat format = new SimpleDateFormat(DEFAULT_STR, Locale.CHINA);
            format.setTimeZone(TimeZone.getTimeZone("GMT+08"));
            return format.format(new Date(date));
        } catch (Exception e) {
            e.printStackTrace();
            return "";
        }
    }

    public static boolean isThan24Hours(String date1, String date2) {
        try {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-M-d HH:mm:ss");
            if ((((double) (sdf.parse(date2).getTime() - sdf.parse(date1).getTime())) * 1.0d) / 3600000.0d >= 24.0d) {
                return true;
            }
            return false;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }
}
